home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Games / Tetris / Source / RCS / Minimatrix.m,v < prev    next >
Encoding:
Text File  |  1975-04-26  |  7.6 KB  |  406 lines

  1. head     1.5;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ;
  6. comment  @@;
  7.  
  8.  
  9. 1.5
  10. date     92.03.01.10.26.34;  author melling;  state Exp;
  11. branches ;
  12. next     1.4;
  13.  
  14. 1.4
  15. date     92.03.01.05.07.25;  author melling;  state Exp;
  16. branches ;
  17. next     1.3;
  18.  
  19. 1.3
  20. date     91.12.16.17.21.55;  author melling;  state Exp;
  21. branches ;
  22. next     1.2;
  23.  
  24. 1.2
  25. date     91.12.07.07.48.38;  author melling;  state Exp;
  26. branches ;
  27. next     1.1;
  28.  
  29. 1.1
  30. date     91.12.07.03.36.38;  author melling;  state Exp;
  31. branches ;
  32. next     ;
  33.  
  34.  
  35. desc
  36. @Tetris 1.1
  37. @
  38.  
  39.  
  40. 1.5
  41. log
  42. @Release 1.3
  43. @
  44. text
  45. @/*
  46.  * This is the view from which NextMatrix and TetMatrix inherit.
  47.  *
  48.  *  The major data structures are.
  49.  *   id *iconMatrix;  Holds the id of the block that is in the current
  50.  *                    (row, column).
  51.  */
  52. #import <appkit/NXImage.h>
  53. #import <appkit/nextstd.h>
  54. #import <dpsclient/psops.h>
  55. #import "Minimatrix.h"
  56.  
  57. @@implementation Minimatrix
  58.  
  59. - initFrame:(const NXRect *)frameRect
  60. {
  61.     return [self initFrame:frameRect bitmap:nil numRows:0 numCols:0];
  62. }
  63.  
  64. - initFrame:(const NXRect *)frameRect
  65.             numRows:(int)rowsHigh numCols:(int)colsWide
  66. {
  67.     return [self initFrame:frameRect
  68.             bitmap:nil
  69.             numRows:rowsHigh numCols:colsWide];
  70. }
  71.  
  72. - initFrame:(const NXRect *)frameRect
  73.        bitmap:theBitmap numRows
  74.         :(int)rowsHigh numCols
  75.         :(int)colsWide
  76. {
  77.     [super initFrame:frameRect];
  78.  
  79.     numRows = rowsHigh;
  80.     numCols = colsWide;
  81.     NX_MALLOC(iconMatrix, id, numRows * numCols);
  82.     backgroundGray = NX_LTGRAY;
  83.     inset.width = inset.height = 0.0;
  84.     insetBounds = bounds;
  85.     intercell = elementSize = inset;
  86.  
  87.     [self setBitmap:theBitmap];
  88.     return self;
  89. }
  90.  
  91. /*
  92.  * Return the id of the bitmap at (row,column) if one exists, else nil.
  93.  */
  94. - bitmapAt:(int)row :(int)column
  95. {
  96.     return (row < 0 || column < 0 || row >= numRows || column >= numCols) ?
  97.       nil : *(iconMatrix + row * numCols + column);
  98. }
  99.  
  100. - displayAt:(int)row :(int)column
  101. {
  102.     NXRect destRect;
  103.  
  104.     NXSetRect(&destRect,
  105.                  inset.width + column * (intercell.width + elementSize.width),
  106.                  inset.height + row * (intercell.width + elementSize.width),
  107.                  elementSize.width + intercell.width,
  108.                  elementSize.height + intercell.height);
  109.  
  110.     return [self display:&destRect :1];
  111. }
  112.  
  113. - drawSelf:(const NXRect *)rects :(int)rectCount
  114. {
  115.     int rowc, colc;
  116.     int rectc;
  117.     NXRect destRect;
  118.     id *iconMatrixPiece;
  119.     const NXRect *rectp;
  120.     static int bsides[] = {NX_YMIN, NX_XMAX, NX_YMAX, NX_XMIN,
  121.                                   NX_YMIN, NX_XMAX, NX_YMAX, NX_XMIN};
  122.  
  123.     static float bgrays[] = {NX_WHITE, NX_WHITE, NX_DKGRAY, NX_DKGRAY,
  124.                                      NX_LTGRAY, NX_WHITE, NX_DKGRAY, NX_DKGRAY};
  125.  
  126.     PSsetgray(backgroundGray);
  127.     NXRectFillList(rects, rectCount); // Clear old areas
  128.  
  129.     destRect = bounds;
  130.  
  131.     // Draw the insetRect around the border of the game when a Piece
  132.     // intersects with the border.
  133.  
  134.     if ((inset.width || inset.height) && !NXContainsRect(&insetBounds, rects)) {
  135.         NXDrawTiledRects(&destRect, (NXRect *)0, bsides, bgrays, 8);
  136.     }
  137.  
  138.     NXSetRect(&destRect, inset.width + intercell.width,
  139.                  inset.height + intercell.height,
  140.                  elementSize.width, elementSize.height);
  141.  
  142.     iconMatrixPiece = iconMatrix;
  143.  
  144.     // Redraw all of the Pieces that have changed
  145.     for (rowc = 0; rowc < numRows; rowc++) {
  146.         for (colc = 0; colc < numCols; colc++) {
  147.             if (*iconMatrixPiece) {
  148.                 rectc = rectCount;
  149.                 rectp = rects;
  150.                 while (rectc--) {
  151.                     if (NXIntersectsRect(&destRect, rectp++)) {
  152.                         // If we assume the piece contains no alpha then we could use
  153.                         // NX_COPY
  154.                         [*iconMatrixPiece composite:NX_SOVER toPoint:&destRect.origin];
  155.                         break;
  156.                     }
  157.                 }
  158.             }
  159.             // Add block's width + space b/w each block to get new X coord
  160.             destRect.origin.x += elementSize.width + intercell.width;
  161.             iconMatrixPiece++;      // Do next column in iconMatrix
  162.         }
  163.         destRect.origin.x = inset.width + intercell.width;
  164.  
  165.         // Add the block's height + intercell.height
  166.         destRect.origin.y += elementSize.height + intercell.height;
  167.     }
  168.     return self;
  169. }
  170.  
  171. /*
  172.  * Private method.
  173.  */
  174. - getIntercell:(NXSize *)aSize
  175. {
  176.     *aSize = intercell;
  177.     return self;
  178. }
  179.  
  180. - getRect:(NXRect *)theRect for:(int)row :(int)column
  181. {
  182.     theRect->size = elementSize; // Get block's image size
  183.     return [self point:&theRect->origin for:row :column];
  184. }
  185.  
  186. /*
  187.  * Calculate the TetrisView coordinate given the Tetrix (row,column)
  188.  */
  189. - point:(NXPoint *)thePoint for:(int)row :(int)column
  190. {
  191.     thePoint->x = inset.width + (column + 1) * intercell.width +
  192.       column * elementSize.width;
  193.     thePoint->y = inset.height + (row + 1) * intercell.height +
  194.       row * elementSize.height;
  195.     return self;
  196. }
  197.  
  198.  
  199. - setBackgroundGray:(float)gray
  200. {
  201.     backgroundGray = gray;
  202.     return self;
  203. }
  204.  
  205. - setBitmap:theBitmap
  206. {
  207.     id *iconMatrixPiece;
  208.  
  209. #ifdef DEBUG
  210.     printf("setBitmap\n");
  211. #endif
  212.  
  213.     iconMatrixPiece = iconMatrix + numRows * numCols;
  214.     while (iconMatrixPiece > iconMatrix)
  215.       *--iconMatrixPiece = theBitmap;
  216.     
  217.     if (theBitmap)
  218.       [theBitmap getSize:&elementSize];
  219.     return self;
  220. }
  221. /*
  222.  * Set the current (row, column) of the iconMatrix equal to the id of
  223.  * the block that was just dropped.
  224.  *
  225.  * There is a bug in this program.  Occasionally, an invalid row
  226.  * is generated.
  227.  *
  228.  * theBitmap row, col = (24, 6)
  229.  * theBitmap row, col = (23, 6)
  230.  * theBitmap row, col = (23, 7)
  231.  * theBitmap row, col = (25, 3)
  232.  * theBitmap row, col = (698590, 3)
  233.  */
  234. - setBitmap:theBitmap at:(int)row :(int)column
  235. {
  236.     if (row < numRows && row >= 0 && column < numCols && column >= 0) {
  237.         *(iconMatrix + row * numCols + column) = theBitmap;
  238.     } else {
  239.         fprintf(stderr, "Invalid row %d in Minimatrix::setBitmap\n", row);
  240.     }
  241.     return self;
  242. }
  243.  
  244.  
  245. /*
  246.  *  The size of the image (TIFF or PS) we are using to build the block.
  247.  *  Currently 16x16 is the image size.
  248.  */
  249. - setElementSize:(const NXSize *)aSize
  250. {
  251.     elementSize = *aSize;
  252.     return self;
  253. }
  254.  
  255. /*
  256.  * Set the number of pixels away from the border the pieces will be.
  257.  */
  258. - setInset:(const NXSize *)aSize
  259. {
  260.     inset = *aSize;
  261.     insetBounds = bounds;
  262.     NXInsetRect(&insetBounds, inset.width, inset.height);
  263.     return self;
  264. }
  265.  
  266. /*
  267.  * Set the amount of space to leave between each cell.
  268.  */
  269. - setIntercell:(const NXSize *)aSize
  270. {
  271.     intercell = *aSize;
  272.     return self;
  273. }
  274.  
  275. - free
  276. {
  277.     NX_FREE(iconMatrix);
  278.     return [super free];
  279. }
  280.  
  281. @@end
  282.  
  283. @
  284.  
  285.  
  286. 1.4
  287. log
  288. @*** empty log message ***
  289. @
  290. text
  291. @@
  292.  
  293.  
  294. 1.3
  295. log
  296. @*** empty log message ***
  297. @
  298. text
  299. @d192 1
  300. a192 1
  301.     if (row<=25 && row>=0) {
  302. @
  303.  
  304.  
  305. 1.2
  306. log
  307. @Tetris 1.2
  308. @
  309. text
  310. @d1 7
  311. a7 1
  312.  
  313. d21 1
  314. a21 1
  315.     numRows:(int)rowsHigh numCols:(int)colsWide
  316. d47 3
  317. a49 1
  318.  
  319. d58 1
  320. a58 1
  321.     NXRect dRect;
  322. d60 1
  323. a60 1
  324.     NXSetRect(&dRect,
  325. d66 1
  326. a66 1
  327.     return [self display:&dRect :1];
  328. d73 2
  329. a74 2
  330.     NXRect dRect;
  331.     id *iconmp;
  332. d78 1
  333. d81 1
  334. a81 1
  335.     
  336. d83 1
  337. a83 1
  338.     NXRectFillList(rects, rectCount);
  339. d85 10
  340. a94 5
  341.     dRect = bounds;
  342.     if ((inset.width || inset.height) && !NXContainsRect(&insetBounds, rects))
  343.       NXDrawTiledRects(&dRect, (NXRect *)0, bsides, bgrays, 8);
  344.     
  345.     NXSetRect(&dRect, inset.width + intercell.width,
  346. d97 4
  347. a100 1
  348.     iconmp = iconMatrix;
  349. d103 1
  350. a103 1
  351.             if (*iconmp) {
  352. d107 4
  353. a110 2
  354.                     if (NXIntersectsRect(&dRect, rectp++)) {
  355.                         [*iconmp composite:NX_COPY toPoint:&dRect.origin];
  356. d115 3
  357. a117 2
  358.             dRect.origin.x += elementSize.width + intercell.width;
  359.             iconmp++;
  360. d119 4
  361. a122 2
  362.         dRect.origin.x = inset.width + intercell.width;
  363.         dRect.origin.y += elementSize.height + intercell.height;
  364. d127 3
  365. d138 1
  366. a138 1
  367.     theRect->size = elementSize;
  368. d142 3
  369. d154 1
  370. d163 1
  371. a163 1
  372.     id *iconmp;
  373. d165 7
  374. a171 3
  375.     iconmp = iconMatrix + numRows * numCols;
  376.     while (iconmp > iconMatrix)
  377.       *--iconmp = theBitmap;
  378. d178 12
  379. a189 8
  380.   There is a bug in this program.  Occasionally, an invalid row
  381.   is generated.
  382. theBitmap row, col = (24, 6)
  383. theBitmap row, col = (23, 6)
  384. theBitmap row, col = (23, 7)
  385. theBitmap row, col = (25, 3)
  386. theBitmap row, col = (698590, 3)
  387. */
  388. d200 5
  389. d211 3
  390. d222 3
  391. @
  392.  
  393.  
  394. 1.1
  395. log
  396. @Initial revision
  397. @
  398. text
  399. @d23 3
  400. a25 1
  401.     bitmap:theBitmap numRows:(int)rowsHigh numCols:(int)colsWide
  402. d75 1
  403. a75 1
  404.     
  405. @
  406.